package com.yb.web.announcement.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.yb.base.web.BaseAction;
import com.yb.bussiness.announcement.AnnouncementService;
import com.yb.bussiness.announcement.AnnouncementServiceImpl;
import com.yb.bussiness.global.SystemLogService;
import com.yb.bussiness.global.SystemLogServiceImpl;
import com.yb.bussiness.goods.CategoryService;
import com.yb.bussiness.goods.CategoryServiceImpl;
import com.yb.domain.goods.TgAnnouncement;
import com.yb.domain.goods.TgCategory;
import com.yb.domain.goods.TgInformation;
import com.yb.util.comm.Contants;
import com.yb.util.comm.DateTimeUtil;
import com.yb.util.comm.RequestUtil;

/**
 *  公告相关Action
 * 
 * @author SFF
 * 
 */
public class AnnouncementAction extends BaseAction {
    
    private static final long serialVersionUID = 688970037309060648L;
    
    private int total;
    
    private TgAnnouncement tgAnnouncement;
    
    private List rows;
    
    private int flag;
    
    private  List<TgCategory> catrows;
    
    private AnnouncementService service;
    
    private CategoryService categoryService;
    
    private SystemLogService systemLogService;
    
    public AnnouncementAction() {
        service = new AnnouncementServiceImpl();
        categoryService = new CategoryServiceImpl();
        systemLogService = new SystemLogServiceImpl();
    }
    
    /*
     * 转向管理页面
     */
    @SuppressWarnings("unchecked")
    public String toAnnouncementManage() {
        catrows = categoryService.findAll(1, 50);
        return "announcementManage";
    }
    
    public String getAnnouncementManage() {
        try {
            String page = RequestUtil.getParam(request, "page", "1");
            String row = RequestUtil.getParam(request, "rows", "10");
            String pcategoryId = RequestUtil.getParam(request, "pcategoryId", "0");
            String categoryId = RequestUtil.getParam(request, "categoryId", "0");
            String type = RequestUtil.getParam(request, "type", "");
            TgAnnouncement formBean = new TgAnnouncement();
            if("".equals(categoryId)){
                formBean.setCategoryId(0);
            }else{
                formBean.setCategoryId(new Integer(categoryId));
            }
            if("".equals(pcategoryId)){
                formBean.setPcategoryId(0);
            }else{
                formBean.setPcategoryId(new Integer(pcategoryId));
            }
          
            formBean.setType(type);
            Map<String, Object> map = service.findAll(formBean, page, row);
            rows = new ArrayList();
            List<TgAnnouncement> list = (List<TgAnnouncement>)map.get("list");
            for (TgAnnouncement tgAnnouncement : list) {
                TgAnnouncement bean = new TgAnnouncement();
                bean.setCategoryId(tgAnnouncement.getCategoryId());
                bean.setContent(tgAnnouncement.getContent());
                bean.setCreateDate(tgAnnouncement.getCreateDate());
                bean.setId(tgAnnouncement.getId());
                bean.setPcategoryId(tgAnnouncement.getPcategoryId());
                bean.setType(tgAnnouncement.getType());
                bean.setCreatTimeStr(DateTimeUtil.switchDateToString(bean.getCreateDate(),"yyyy-MM-dd HH:mm:ss"));
                String cname = "";
                String pcname = "";
                
                
                TgCategory cbean = categoryService.findById(new Long(tgAnnouncement.getPcategoryId() + ""));
                if (cbean != null) {
                    pcname = cbean.getName();
                    bean.setCategoryName(pcname);
                }
                cbean = categoryService.findById(new Long(tgAnnouncement.getCategoryId() + ""));
                if (cbean != null) {
                    cname= cbean.getName();
                    bean.setCategoryName(pcname+"--"+cname);
                }
                rows.add(bean);
            }
            
            total = Integer.parseInt(map.get("total").toString());
            
        }
        catch (Exception e) {
            log.error("公告管理信息出错", e);
        }
        return Contants.JSONSTRING_RESULT_NAME;
    }
    
    public String saveOrUpdate() {
        try {
            tgAnnouncement.setCreateDate(new Date());
            service.saveOrUpdate(tgAnnouncement);
            
            String str = "";
            if("0".equals(tgAnnouncement.getType()))str = "拼购公告";
            if("1".equals(tgAnnouncement.getType()))str = "拼抢公告";
            if("2".equals(tgAnnouncement.getType()))str = "曝光维权公告";
            if("3".equals(tgAnnouncement.getType()))str = "论坛公告";
            String pcname = "";
            String cname = "";
            TgCategory cbean = categoryService.findById(new Long(tgAnnouncement.getPcategoryId() + ""));
            if (cbean != null) {
                pcname = cbean.getName();
            }
            cbean = categoryService.findById(new Long(tgAnnouncement.getCategoryId() + ""));
            if (cbean != null) {
                cname= cbean.getName();
                pcname = pcname+"--"+cname;
            }
            if(tgAnnouncement.getId() == null){
                systemLogService.saveSystemLog(request, "公告管理", "新增--"+str+"-"+pcname);
            }else{
                systemLogService.saveSystemLog(request, "公告管理", "修改--"+str+"-"+pcname);  
            }
            tgAnnouncement = null;
        }
        catch (Exception e) {
            log.error("保存或修改信息出错", e);
        }
        return "toAnnouncementManage";
    }
    
    public String delete() {
        String id = RequestUtil.getParam(request, "id", "");
        String idString = RequestUtil.getParam(request, "idString", "");
        
        tgAnnouncement = service.read(new Long(id));
        String str = "";
        if("0".equals(tgAnnouncement.getType()))str = "拼购公告";
        if("1".equals(tgAnnouncement.getType()))str = "拼抢公告";
        if("2".equals(tgAnnouncement.getType()))str = "曝光维权公告";
        if("3".equals(tgAnnouncement.getType()))str = "论坛公告";
        String pcname = "";
        String cname = "";
        TgCategory cbean = categoryService.findById(new Long(tgAnnouncement.getPcategoryId() + ""));
        if (cbean != null) {
            pcname = cbean.getName();
        }
        cbean = categoryService.findById(new Long(tgAnnouncement.getCategoryId() + ""));
        if (cbean != null) {
            cname= cbean.getName();
            pcname = pcname+"--"+cname;
        }
        systemLogService.saveSystemLog(request, "公告管理", "删除--"+str+"-"+pcname);  
        tgAnnouncement = null;
        
        boolean isFlag = false;
        if (!"".equals(id))
            isFlag = service.deleteByIdString(id);
        else
            isFlag = service.deleteByIdString(idString);
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = null;
        if (isFlag)
            message = "删除成功";
        else
            message = "删除失败";
        StringBuffer sb = new StringBuffer("[");
        sb.append("{\"message\":\"" + message + "\"" + "}");
        sb.append("]");
        
       
        
        try {
            out = response.getWriter();
            out.println(sb.toString());
        }
        catch (IOException e) {
            log.error("删除信息出错", e);
        }
        return null;
    }
    
    /*
     * 跳转信息页面
     */
    public String toAnnouncementInfo() {
        try {
            
            long id = RequestUtil.getParam(request, "id", 0l);
            if (id!=0) {
                tgAnnouncement = service.read(id);
                tgAnnouncement.setCreatTimeStr(DateTimeUtil.switchDateToString(tgAnnouncement.getCreateDate(),"yyyy-MM-dd HH:mm:ss"));
            }
            List<TgCategory> cList = new ArrayList();
            List<TgCategory> pList = categoryService.findAll(1, 50);
            if(tgAnnouncement!=null){
             TgCategory tc = categoryService.findById(new Long(tgAnnouncement.getCategoryId()));
             if(tc!=null){
                 cList = categoryService.findByPid(new Long(tc
                     .getPid()), -1, -1);
               }
             ActionContext.getContext().put("pid", tgAnnouncement.getPcategoryId());
             ActionContext.getContext().put("cid", tgAnnouncement.getCategoryId());
            }else{
                ActionContext.getContext().put("pid", "0");
                ActionContext.getContext().put("cid", "0");
            }
            ActionContext.getContext().put("pList", pList);
            ActionContext.getContext().put("cList", cList);
            ActionContext.getContext().put("bid", id);
           
            
        }
        catch (Exception e) {
            log.error("查询信息出错", e);
        }
        return "toAnnouncementInfo";
    }
    
    /**
     * 获取系统路径
     * 
     * @return
     */
    private String getBasePath() {
        return request.getSession().getServletContext().getRealPath("");
    }
    
    public int getTotal() {
        return total;
    }
    
    public void setTotal(int total) {
        this.total = total;
    }
    
    public List getRows() {
        return rows;
    }
    
    public void setRows(List rows) {
        this.rows = rows;
    }
    
    public int getFlag() {
        return flag;
    }
    
    public void setFlag(int flag) {
        this.flag = flag;
    }
    
    public TgAnnouncement getTgAnnouncement() {
        return tgAnnouncement;
    }
    
    public void setTgAnnouncement(TgAnnouncement tgAnnouncement) {
        this.tgAnnouncement = tgAnnouncement;
    }
    
    public AnnouncementService getService() {
        return service;
    }
    
    public void setService(AnnouncementService service) {
        this.service = service;
    }

    public List<TgCategory> getCatrows() {
        return catrows;
    }

    public void setCatrows(List<TgCategory> catrows) {
        this.catrows = catrows;
    }

    public CategoryService getCategoryService() {
        return categoryService;
    }

    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }
    
    
    
}
